Can we return document.createElement("element")?
NickName:sharon Ask DateTime:2015-12-17T23:49:39

Can we return document.createElement("element")?

I have to create an input box multiple times, so I call a function like this multiple times:

function __createInputBox(id) {
    var input = document.createElement("input");

    input.setAttribute('id', id);
    input.setAttribute('type', 'number');
    input.setAttribute('name', id);
    input.setAttribute('value', '0');
    input.setAttribute('min', '0');

    return input;
}

In my main function, I append it as such:

var box = __createInputBox(id);
element.appendChild(box);

I keep getting this error:

__createInputBox is not defined

So are we allowed to return the value from document.createElement("element")? If its bad to do so, what is the better way to add multiple elements to the page?

This how I declare the function:

function InputSpace(){
this.inputSpace = document.getElementById("inputspace");
this.num = 1;

    function __createInputBox(id) {
       // function  declared here
    }

This is the code where I call it:

InputSpace.prototype = {
constructor: InputSpace,
drawInputSpace : function () {
  var i = 0,
      max;
  var table = document.createElement("TABLE");
  var table_body = document.createElement("TBODY");
  for(max = num; i<num; i++){
        var element = document.createElement("TR");
        var box = __createInputBox(id);
        element.appendChild(box);
        table_body.appendChild(element);
  }
  table.appendChild(table_body);
  this.inputSpace.appendChild(table);
}

Copyright Notice:Content Author:「sharon」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34338706/can-we-return-document-createelementelement

More about “Can we return document.createElement("element")?” related questions

Can we return document.createElement("element")?

I have to create an input box multiple times, so I call a function like this multiple times: function __createInputBox(id) { var input = document.createElement("input"); input.setAttribut...

Show Detail

Can document.createElement be combined

Can multiple elements be created without rewriting document.createElement() each time.. document.createElement("article"); document.createElement("footer"); document.createElement("header"); docum...

Show Detail

Multiple document.createElement with eventListeners in One Function Return?

I have a function that returns something into the dom (a cellrenderer in ag-grid). Is it possible to create two different elements with document.createElement(), attach event listeners with

Show Detail

document.createElement variable return from a function as undefined. Why?

I have this code: /* Creating whole HTML Units from HTML Element list */ function createHTMLUnit(unitstruct){ var tempElement; Object.entries(unitstruct).forEach(([key, value]) =&gt; { //

Show Detail

document.createElement variable return from a function as undefined. Why?

I have this code: /* Creating whole HTML Units from HTML Element list */ function createHTMLUnit(unitstruct){ var tempElement; Object.entries(unitstruct).forEach(([key, value]) =&gt; { //

Show Detail

How can we return a customer created element in JS to the render of the webcomponent in stenciljs

I have a component which has a custom method to create an element, I create a custom element using js and return the array how can we use this array or elements/element to the be returned as a render

Show Detail

How to refer to a document.createElement in CSS

I'm selecting data from an API which i then make into Objects with document.createElement but how can i style them? Here's my Js code: fetch('https://randomuser.me/api/?results=5&amp;nat=us').t...

Show Detail

What is the behavior of document.createElement when passed as an argument?

When attempting to pass an element created by the document.createElement method, we encounter unusual results: var Todo = (function () { return { items: [], addItem: function () {...

Show Detail

document.CreateElement Implementation

I need some help with JavaScript. As while i was working on use of XML with JavaScript , i have come through lots of problem. So i want to use document.createElement("ul") document.createElement("u...

Show Detail

document.createElement not working

I am creating a plugin using jQuery library. Here i am storing String.prototype in a variable then i am using this variable to extend my Sting object. And this is working fine. // String Prototyp...

Show Detail